package com.b2b.controller;


import com.b2b.model.CategoryDto;
import com.b2b.model.common.ResponseBean;

import com.b2b.model.entity.goods.Category;
import com.b2b.service.goodsService.CategoryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@RequestMapping("/category")
@PropertySource("classpath:config.properties")
public class CategoryController {

    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    @Autowired
    CategoryService categoryService;

    @PostMapping("/add")
    public ResponseBean add(@RequestBody CategoryDto categoryDto) {
        if(categoryDto.getParentId() != 0) {
            categoryDto.setCategoryLevel(categoryService.selectByPrimaryKey(categoryDto.getParentId()).getCategoryLevel() + 1);
        }else {
            categoryDto.setCategoryLevel(0);
        }
        categoryService.insert(categoryDto);
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", null);
    }

    @DeleteMapping("/delete")
    public ResponseBean delete(@RequestParam Map<String, String> categoryIdMap){
        Integer categoryId = Integer.parseInt(categoryIdMap.get("categoryId"));
        categoryService.deleteByPrimaryKey(categoryId);
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }

    @PutMapping("/update")
    public ResponseBean update(@RequestBody CategoryDto categoryDto){
        categoryService.updateByPrimaryKeySelective(categoryDto);
        return new ResponseBean(HttpStatus.OK.value(), "修改成功(Update Success)", null);
    }

    @GetMapping("/get")
    public ResponseBean getByParentId(@RequestParam Object parentId){
        CategoryDto categoryDto = new CategoryDto();
        String parent = (String) parentId;
        categoryDto.setParentId(Integer.parseInt(parent));
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Insert Success)", categoryService.select(categoryDto));
    }

    @GetMapping("/getAll")
    public ResponseBean getAll(){

        CategoryDto categoryDtoTemp = new CategoryDto();
        categoryDtoTemp.setParentId(0);
        List<CategoryDto> categoryDtoList = categoryService.select(categoryDtoTemp);

        for (CategoryDto c1:categoryDtoList
             ) {
            CategoryDto categoryDto1 = new CategoryDto();
            categoryDto1.setParentId(c1.getCategoryId());
            List<CategoryDto> categoryDtoListTemp = categoryService.select(categoryDto1);
            for (CategoryDto c2: categoryDtoListTemp
            ) {
                CategoryDto categoryDto2 = new CategoryDto();
                categoryDto2.setParentId(c2.getCategoryId());
                c2.setChildrenList(categoryService.select(categoryDto2));
            }
            c1.setChildrenList(categoryDtoListTemp);
        }

        return new ResponseBean(HttpStatus.OK.value(), "查询成功", categoryDtoList);
    }
}
